﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using mshtml;
using HtmlAgilityPack;

public class Parse
{
	public class NaverTitle_m
	{
		public string address {get; set; }
		public string name { get; set; }
		public string author { get; set; }
		public string time { get; set; }
		public string titleId { get; set; }
	}
	
	public class NaverList_m
	{
		public string address { get; set; }
		public string name { get; set; }
		public string date { get; set; }
	}
	
	public class NaverContent_m
	{
		public string address { get; set; }
		public string prev { get; set; }
		public string next { get; set; }
		public string title { get; set; }
		public string list { get; set; }
	}

	public class NaverTitle
	{
		public string address { get; set; }
		public string name { get; set; }
		public string author { get; set; }
		public string time { get; set; }
		public string titleId { get; set; }
	}

	public class NaverList
	{
		public string address { get; set; }
		public string name { get; set; }
		public string date { get; set; }
	}
	
	public class NaverContent
	{
		public string address { get; set; }
		public string prev { get; set; }
		public string next { get; set; }
		public string title { get; set; }
		public string list { get; set; }
	}
	
	public Parse()
	{
	}
	
	public List<NaverTitle_m> parseNaverTitle_m(List<NaverTitle_m> list, string week)
	{
		return parseNaverTitle_m(list, week, 1);
	}
	
	public List<NaverTitle_m> parseNaverTitle_m(List<NaverTitle_m> list, string week, int page)
	{
		string url;
		
		if(page > 1)
			url = String.Format("http://m.comic.naver.com/webtoon/weekday.nhn?week={0}&page={1}", week, page);
		else 
			url = String.Format("http://m.comic.naver.com/webtoon/weekday.nhn?week={0}", week);

		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc = web.Load(url);
		HtmlNodeCollection hnc = doc.GetElementbyId("pageList").ChildNodes;
		NaverTitle_m n;
		
		foreach (HtmlNode h in hnc)
		{
			if(h.Name == "#text")
				continue;
			else if(h.Name == "li")
			{
				n = new NaverTitle_m()
				{
					address = h.SelectNodes("a")[0].GetAttributeValue("href", ""),
					name = h.SelectNodes("a/span/span/strong")[0].InnerText,
					author = h.SelectNodes("a/span/span[@class='if1']")[0].InnerText,
					time = h.SelectNodes("a/span[@class='cor']")[0].InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "").Trim()
				};
				list.Add(n);
			}
		}
		
		if( doc.GetElementbyId("nextButton") != null )
		{
			list = parseNaverTitle_m(list, week, page + 1);
		}
		
		return list;
	}
	
	public List<NaverTitle> parseNaverTitle(List<NaverTitle> list, string week)
	{
		string url;
		url = String.Format("http://comic.naver.com/webtoon/weekdayList.nhn?week={0}&view=list", week);
		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc  = web.Load(url);
		HtmlNodeCollection hnc = doc.GetElementbyId("content").SelectSingleNode("div[@class='list_area table_list_area']/table/tbody").SelectNodes("tr");
		NaverTitle n;
		
		foreach (HtmlNode h in hnc)
		{
			n = new NaverTitle()
			{
				address = h.SelectSingleNode("td[@class='subject']/a").GetAttributeValue("href", ""),
				name = h.SelectSingleNode("td[@class='subject']/a/strong").InnerText,
				author = h.SelectNodes("td")[2].SelectSingleNode("a").InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", ""),
				time = h.SelectSingleNode("td[@class='date']").InnerText
			};
			list.Add(n);
		}
		return list;
	}
	
	
	public List<NaverList_m> parseNaverList_m(List<NaverList_m> list, string titleId)
	{
		return parseNaverList_m(list, titleId, 1);
	}
	
	public List<NaverList_m> parseNaverList_m(List<NaverList_m> list, string titleId, int page)
	{
		string url;
		url = String.Format("http://m.comic.naver.com/webtoon/list.nhn?titleId={0}&page={1}", titleId, page);
		
		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc = web.Load(url);
		HtmlNodeCollection hnc = doc.GetElementbyId("pageList").ChildNodes;
		NaverList_m n;
		
		foreach (HtmlNode h in hnc)
		{
			if(h.Name == "#text")
				continue;
			else if(h.Name == "li")
			{
				n = new NaverList_m()
				{
					address = h.SelectNodes("a")[0].GetAttributeValue("href", ""),
					name = h.SelectNodes("a/span/span[@class='wn']")[0].InnerText,
					date = h.SelectNodes("a/span/span[@class='if1 tx2']")[0].InnerText
				};
				list.Add(n);
			}
		}
		return list;
	}
	
	public List<NaverList> parseNaverList(List<NaverList> list, string titleId)
	{
		string url;
		url = String.Format("http://comic.naver.com/webtoon/list.nhn?titleId={0}", titleId);
		
		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc = web.Load(url);
		HtmlNodeCollection hnc = doc.GetElementbyId("content").SelectNodes("table/tr");
		NaverList n;
		
		foreach (HtmlNode h in hnc)
		{
			if(h.SelectSingleNode("th") != null || h.SelectSingleNode("td[@class='blank']") != null)
				continue;
			n = new NaverList()
			{
				address = h.SelectSingleNode("td[@class='title']/a").GetAttributeValue("href", ""),
				name = h.SelectSingleNode("td[@class='title']/a").InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", ""),
				date = h.SelectSingleNode("td[@class='num']").InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "")
			};
			list.Add(n);
		}
		
		return list;
	}
	
	public List<NaverContent_m> parseNaverContent_m(List<NaverContent_m> list, string titleId, int no)
	{
		return parseNaverContent_m(list, titleId, no, 1);
	}
	
	public List<NaverContent_m> parseNaverContent_m(List<NaverContent_m> list, string titleId, int no, int page)
	{
		string url;
		url = String.Format("http://m.comic.naver.com/webtoon/detail.nhn?titleId={0}&no={1}&page={2}", titleId, no, page);
		
		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc = web.Load(url);
		HtmlNodeCollection hnc;
		NaverContent_m n;
		
		if(page == 1)
		{
			n = new NaverContent_m()
			{
				title = doc.GetElementbyId("ct").SelectSingleNode("div[@class='chh']/h1").InnerText
			};
		
			hnc = doc.GetElementbyId("ct").SelectSingleNode("div/p[@class='bt7']").SelectNodes("a");
			string prev_add = "", next_add = "", list_add = "";
			foreach (HtmlNode h in hnc)
			{
				switch( h.InnerText )
				{
					case "이전화" : 
						prev_add = h.GetAttributeValue("href", "");
						break;
					case "다음화" : 
						next_add = h.GetAttributeValue("href", "");
						break;
					default : 
						list_add = h.GetAttributeValue("href", "");
						break;
				}
			}
			n.prev = prev_add;
			n.next = next_add;
			n.list = list_add;
			
			list.Add(n);
		}
		
		hnc = doc.GetElementbyId("ct").SelectNodes("div[@class='im_c']");
		foreach (HtmlNode h in hnc)
		{
			n = new NaverContent_m()
			{
				address = h.SelectNodes("img")[0].GetAttributeValue("src", "")
			};
			list.Add(n);
		}

		if (doc.GetElementbyId("nextButton") != null)
		{
			list = parseNaverContent_m(list, titleId, no, page + 1);
		}
		
		return list;
	}
	
	public List<NaverContent> parseNaverContent(List<NaverContent> list, string titleId, int no)
	{
		string url;
		url = String.Format("http://comic.naver.com/webtoon/detail.nhn?titleId={0}&seq={1}", titleId, no);

		HtmlWeb web = new HtmlWeb();
		HtmlDocument doc = web.Load(url);
		
		string content = doc.DocumentNode.OuterHtml;
		int pos1 = content.IndexOf("var imageList = [");
		int pos2 = content.IndexOf("var imageWidth") - pos1;
		string[] imgList = content.Substring(pos1, pos2).Trim().Replace("var imageList = [", "").Replace("]","").Replace("\"","").Replace(";", "").Split(',');
		
		HtmlNodeCollection hnc;
		NaverContent n;

		n = new NaverContent()
		{
			title = doc.GetElementbyId("content").SelectSingleNode("div[@class='section_spot']/div[@class='tit_area']/div[@class='view']").InnerText.Replace("\r", "").Replace("\t", "").Replace("\n", "")
		};

		hnc = doc.GetElementbyId("content").SelectSingleNode("div[@class='section_spot']/div[@class='tit_area']/div[@class='view']/div[@class='btn_area']").SelectNodes("span");
		string prev_add = "", next_add = "";
		foreach (HtmlNode h in hnc)
		{
			switch (h.GetAttributeValue("class", ""))
			{
				case "pre":
					prev_add = h.SelectSingleNode("a").GetAttributeValue("href", "");
					break;
				case "next":
					next_add = h.SelectSingleNode("a").GetAttributeValue("href", "");
					break;
			}
		}
		
		n.prev = prev_add;
		n.next = next_add;
		n.list = doc.GetElementbyId("content").SelectSingleNode("div[@class='section_spot']/div[@class='comicinfo']/div[@class='dsc']/ul/li[2]/a").GetAttributeValue("href", ""); ;

		list.Add(n);

		foreach (string s in imgList)
		{
			n = new NaverContent()
			{
				address = s
			};
			list.Add(n);
		}

		return list;
	}
}
